#include "Characters/Combat/CombatManager.h"
#include "GameFramework/Character.h"
#include "EnhancedInputComponent.h"

ACombatManager* ACombatManager::CombatManagerInstance = nullptr;

ACombatManager::ACombatManager()
{
	PrimaryActorTick.bCanEverTick = true;
}

void ACombatManager::BeginPlay()
{
	Super::BeginPlay();
}

void ACombatManager::ProcessInputActionBinding()
{
	ACharacter* Character = Cast<ACharacter>(GetOwner());

	if(Character)
	{
		UEnhancedInputComponent* LocalInputComponent= Cast<UEnhancedInputComponent>(Character->InputComponent);
		if(LocalInputComponent)
		{
			LocalInputComponent->BindAction(IA_AttackLight, ETriggerEvent::Triggered, this, &ACombatManager::OnAttackLightTriggered);
		}
	}
	// if (UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent))
	// {	
	// 	Input->BindAction(MoveInputAction, ETriggerEvent::Triggered, this, &AMoverExamplesCharacter::OnMoveTriggered);
}

void ACombatManager::OnAttackLightTriggered(const FInputActionValue& Value)
{
	UE_LOG(LogTemp, Log, TEXT("OnAttackLightTriggered %s"), Value.Get<bool>() ? TEXT("true") : TEXT("false"));
}

void ACombatManager::OnAttackHeavyTriggered(const FInputActionValue& Value)
{
	UE_LOG(LogTemp, Log, TEXT("OnAttackHeavyTriggered %s"), Value.Get<bool>() ? TEXT("true") : TEXT("false"));
}

void ACombatManager::OnDefenseTriggered(const FInputActionValue& Value)
{
	UE_LOG(LogTemp, Log, TEXT("OnDefenseTriggered %s"), Value.Get<bool>() ? TEXT("true") : TEXT("false"));
}

void ACombatManager::OnParryTriggered(const FInputActionValue& Value)
{
	UE_LOG(LogTemp, Log, TEXT("OnParryTriggered %s"), Value.Get<bool>() ? TEXT("true") : TEXT("false"));
}

void ACombatManager::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

ACombatManager* ACombatManager::Get()
{
		if (!CombatManagerInstance)
		{
			// Find the Blueprint class
			static ConstructorHelpers::FClassFinder<ACombatManager> SingletonBPClass(TEXT("/Game/Design/Combat/BP_PlayerCombatManager.BP_PlayerCombatManager"));
			if (SingletonBPClass.Succeeded())
			{
				CombatManagerInstance = NewObject<ACombatManager>(GetTransientPackage(), SingletonBPClass.Class);
				CombatManagerInstance->AddToRoot(); // Prevents garbage collection
			}
		}
	
	return CombatManagerInstance;
}

const UDataTable* ACombatManager::GetCombatSchemesDataTable()
{
	ACombatManager* PlayerCombatManager = Get();
	return PlayerCombatManager->CombatSchemesDataTable;
}

